package com.jrx.anytxn.transaction.job.dailyAccount.config;

import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.entity.TlDailyAccountLog;
import com.jrx.anytxn.transaction.job.dailyAccount.bean.DailyAccountBean;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountStatusUpdateProcessor;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountStatusUpdateReader;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountStatusUpdateWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;

/**
 * 日终T24核心汇总记账 记账状态更新
 * @Author yhy
 * @Date 2023/4/29 14:54
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class DailyAccountStatusUpdateConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private DailyAccountStatusUpdateProcessor dailyAccountStatusUpdateProcessor;

    @Autowired
    private DailyAccountStatusUpdateWriter dailyAccountStatusUpdateWriter;

    @Bean
    public Job dailyAccountStatusUpdateJob() {
        return jobBuilderFactory.get("dailyAccountStatusUpdateJob")
                .start(dailyAccountStatusUpdateStep())
                .build();
    }

    @Bean
    public Step dailyAccountStatusUpdateStep() {
        return stepBuilderFactory.get("dailyAccountStatusUpdateStep")
                .<TlDailyAccountLog, DailyAccountBean>chunk(anytxnBatchProperties.getDailyAccountConfiguration().getChunkLimit())
                //表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(dailyAccountStatusUpdateReader())
                .processor(dailyAccountStatusUpdateProcessor)
                .writer(dailyAccountStatusUpdateWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDailyAccountConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlDailyAccountLog> dailyAccountStatusUpdateReader() {
        return new DailyAccountStatusUpdateReader(sqlSessionFactory, anytxnBatchProperties.getDailyAccountConfiguration().getPageSize());
    }
}
